
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bean;

import game.Table;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import org.icefaces.application.PushRenderer;
import other.Costants;

/**
 *
 * @author alessandro
 */
@ManagedBean
@ApplicationScoped
public class TablesManager implements Serializable {
    List<Table> tableList = new ArrayList<Table>();
    @ManagedProperty(value = "#{messageBean}")
    private MessageBean messageBean;


    public TablesManager() {
        //PushRenderer.addCurrentSession(Costants.PUSH_TABLE);
    }

    public MessageBean getMessageBean() {
        return messageBean;
    }

    public void setMessageBean(MessageBean messageBean) {
        this.messageBean = messageBean;
    }

    public List<Table> getTableList() {
        return tableList;
    }

    public void setTableList(List<Table> tableList) {
        this.tableList = tableList;
    }

    public String addTable(String creator, int bots, String name) {
        if(!this.tableExist(creator)) {
            tableList.add(new Table(creator, bots, name));
            System.out.println("Tavolo creato \n Creatore: "+ creator +"\n Numero bots: "+bots);
            return Costants.TABLE;
        } else
            return null;
    }

    public void addWatcher(String user, String table) {
        for(int i=0; i < tableList.size(); i++) {
            if(tableList.get(i).getName().equals(table))
                tableList.get(i).setNewUserWatching(user);
        }
    }

    public int getTableId(String user) {
        int id = -1;

        try {
        for(int i=0; i< tableList.size(); i++){
            for(int j=0; j< tableList.get(i).getUserWatchingNumber(); j++) {
                if(tableList.get(i).getUserWatching().get(j).equals(user)) {
                    return i;
                }
            }
            for(int j=0; j< tableList.get(i).getUserPlayingNumber(); j++) {
                if(tableList.get(i).getUserPlaying().get(j).equals(user)) {
                    return i;
                }
            }
        }
        } catch (Exception e) {

        }

        return id;
    }

    public Table getTableByUser(String user) {
        Table table = null;
        try {
        for(int i=0; i< tableList.size(); i++){
            for(int j=0; j< tableList.get(i).getUserWatchingNumber(); j++) {
                if(tableList.get(i).getUserWatching().get(j).equals(user)) {
                    return tableList.get(i);
                }
            }
            for(int j=0; j< tableList.get(i).getUserPlayingNumber(); j++) {
                if(tableList.get(i).getUserPlaying().get(j).equals(user)) {
                    return tableList.get(i);
                }
            }
        }
        } catch (Exception e) {

        }

        return table;
    }

    public boolean tableExist(String creator) {
        boolean flag = false;
        if(this.getTableByUser(creator) != null) {
            flag = true;
        }

        return flag;
    }

    public void addPlayer(String username) {
        Table table = this.getTableByUser(username);
        if (!(table.isPlaying(username))) {
            if (table.isWatching(username)) {
                table.revomeUserWatching(username);
            }
            table.addUserPlaying(username);
            this.setMessageTemp(username, table, Costants.TABLE_NEW_PLAYER + " " + username, Costants.TABLE_TIME_STANDARD);
            tableList.set(this.getTableId(username), table);
        }
    }

    public void removeWatcher(String username) {
        Table table = this.getTableByUser(username);
        if(table.isWatching(username)) {
           table.revomeUserWatching(username);
           //tableList.set(this.getTableId(username), table);

        }
        PushRenderer.render(Costants.PUSH_GROUP);
        
    }

    public void leaveTable(String user) {
        try {
            Table table = this.getTableByUser(user);
            
            if (table.getCreator().equals(user)) {
                System.out.println("RIMUOVO TAVOLO DI " + user);
                tableList.remove(table);
                messageBean.removeTable(user);
            } else if (table.isPlaying(user)) {
                table.removeUserPlaying(user);
                tableList.set(this.getTableId(user), table);
            } else if (table.isWatching(user)) {
                table.revomeUserWatching(user);
                tableList.set(this.getTableId(user), table);
            }
            
        } catch (Exception e) {
        }

    }

    public void setMessageTemp(String username, Table table, String message, int time) {
        String messageTemp = table.getMessage();
        table.setMessage(message);
        tableList.set(this.getTableId(username), table);
        PushRenderer.render(Costants.PUSH_TABLE);

        try {
            Thread.sleep(time);
        } catch (InterruptedException ex) {
            System.out.println("PROBLEMA CON LO SLEEP");
        }
        table.setMessage(messageTemp);
        tableList.set(this.getTableId(username), table);
        PushRenderer.render(Costants.PUSH_TABLE);
    }


}
